package com.salton123.view;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
import android.widget.LinearLayout;

import androidx.annotation.Nullable;
import androidx.viewpager.widget.ViewPager;

import net.lucode.hackware.magicindicator.MagicIndicator;
import net.lucode.hackware.magicindicator.ViewPagerHelper;
import net.lucode.hackware.magicindicator.buildins.UIUtil;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.CommonNavigator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.CommonNavigatorAdapter;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.abs.IPagerTitleView;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.indicators.LinePagerIndicator;
import net.lucode.hackware.magicindicator.buildins.commonnavigator.titles.badge.BadgePagerTitleView;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/**
 * User: newSalton@outlook.com
 * Date: 2019/7/19 9:56
 * ModifyTime: 9:56
 * Description:
 */
public class ScaledAnimationTitleIndicator extends LinearLayout {
    private MagicIndicator mMagicIndicator;
    private CommonNavigator mCommonNavigator;
    private CommonNavigatorAdapter mCommonNavigatorAdapter;
    private List<String> mTitleList = new ArrayList<>();
    private ViewPager mViewPager;

    public ScaledAnimationTitleIndicator(Context context) {
        super(context);
        initScaledAnimationTitleIndicator();
    }

    public ScaledAnimationTitleIndicator(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initScaledAnimationTitleIndicator();
    }

    public ScaledAnimationTitleIndicator(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initScaledAnimationTitleIndicator();
    }

    public void setTitles(List<String> titles) {
        this.mTitleList = titles;
        notifyDataSetChanged();
    }

    public void setTitles(String[] titles) {
        this.mTitleList = Arrays.asList(titles);
        notifyDataSetChanged();
    }

    public void notifyDataSetChanged() {
        mCommonNavigatorAdapter.notifyDataSetChanged();
    }

    public void initScaledAnimationTitleIndicator() {
        mMagicIndicator = new MagicIndicator(getContext());
        LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        addView(mMagicIndicator, params);
        // mMagicIndicator.setBackgroundColor(Color.WHITE);
        mCommonNavigator = new CommonNavigator(getContext());
        mCommonNavigatorAdapter = new CommonNavigatorAdapter() {
            @Override
            public int getCount() {
                return mTitleList == null ? 0 : mTitleList.size();
            }

            @Override
            public IPagerTitleView getTitleView(Context context, final int index) {
                BadgePagerTitleView badgePagerTitleView = new BadgePagerTitleView(context);
                ScaleTransitionPagerTitleView simplePagerTitleView = new ScaleTransitionPagerTitleView(context);
                simplePagerTitleView.setText(mTitleList.get(index));
                simplePagerTitleView.setTextSize(15);
                simplePagerTitleView.setSelectedBold(true);
                simplePagerTitleView.setNormalColor(Color.parseColor("#b3b3b3"));
                simplePagerTitleView.setSelectedColor(Color.parseColor("#fefefe"));
                simplePagerTitleView.setOnClickListener(new OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        if (mViewPager != null) {
                            mViewPager.setCurrentItem(index);
                        }
                    }
                });
                badgePagerTitleView.setInnerPagerTitleView(simplePagerTitleView);
                return badgePagerTitleView;
            }

            @Override
            public IPagerIndicator getIndicator(Context context) {
                LinePagerIndicator indicator = new LinePagerIndicator(context);
                indicator.setColors(Color.parseColor("#9f6def"));
                indicator.setStartInterpolator(new AccelerateInterpolator());
                indicator.setEndInterpolator(new DecelerateInterpolator(1.6f));
                indicator.setLineHeight(UIUtil.dip2px(context, 2));
                indicator.setLineWidth(UIUtil.dip2px(context, 20));
                indicator.setMode(LinePagerIndicator.MODE_EXACTLY);
                return indicator;
            }
        };
        mCommonNavigator.setAdapter(mCommonNavigatorAdapter);
        mMagicIndicator.setNavigator(mCommonNavigator);
    }

    public void bindPager(ViewPager viewPager) {
        mViewPager = viewPager;
        ViewPagerHelper.bind(mMagicIndicator, mViewPager);
    }
}
